package cn.com.bdqn.controller;

import cn.com.bdqn.entity.SalPlan;
import cn.com.bdqn.service.SalPlanService;
import cn.com.bdqn.utils.ReturnData;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/salPlan")
@Api(tags = "客户开发计划管理接口")
public class SalPlanController {

    @Autowired
    private SalPlanService salPlanService;

    @Operation(summary = "根据销售机会ID查询客户开发计划")
    @PostMapping("/queryByPlaChcId")
    public ReturnData queryByPlaChcId(Long plaChcId){
        try {
            List<SalPlan> salPlans = salPlanService.queryByPlaChcId(plaChcId);
            return ReturnData.ok().put("salPlans",salPlans);
        }catch (Exception e){
            return ReturnData.currError("查询开发计划异常");
        }
    }

    @Operation(summary = "新增客户开发计划")
    @PostMapping("/addSalPlan")
    public ReturnData addSalPlan(SalPlan salPlan){
        int result = salPlanService.addSalPlan(salPlan);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.error();
        }

    }

    @Operation(summary = "修改客户开发计划")
    @PostMapping("/updateSalPlan")
    public ReturnData updateSalPlan(SalPlan salPlan){
        int result = salPlanService.updateSalPlan(salPlan);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.error();
        }
    }

    @Operation(summary = "删除客户开发计划")
    @PostMapping("/delSalPlan")
    public ReturnData delSalPlan(Long plaId){
        int result = salPlanService.delSalPlan(plaId);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.error();
        }
    }

    @Operation(summary = "根据客户开发计划编号查询")
    @PostMapping("/queryBySalId")
    public ReturnData queryBySalId(Long plaId){
        try {
            SalPlan salPlan = salPlanService.queryByPlaId(plaId);
            return ReturnData.ok().put("salPlanse",salPlan);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnData.currError("数据库异常");
        }
    }
}
